package br.com.sig.financeiro.fundofixo.presentation.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.sig.criteria.DateCriteriaVisitor;
import br.com.sig.financeiro.fundofixo.business.model.bo.FundoFixoBO;
import br.com.sig.financeiro.fundofixo.business.model.entity.FundoFixo;
import br.com.sig.presentation.action.BaseListAction;

@Named
@ViewScoped
public class FundoFixoList extends BaseListAction<FundoFixo> {
	
	private static final long serialVersionUID = -6702382405618377244L;
	
	private Date dataDeInicio = null;
	private Date dataDeTermino = null;	
	
	@Inject
	private FundoFixoBO fundoFixoBO;	

	@PostConstruct
	public void init() {
		setService(fundoFixoBO);
		setSortField("data");
	}
	
	@Override
	public void find() {
		this.visitor = new DateCriteriaVisitor("data", dataDeInicio, dataDeTermino);		
		super.find();
	}
	
	@Override
	protected FundoFixo createInstance() {
		this.dataDeInicio = null;
		this.dataDeTermino = null;
		return super.createInstance();
	}
	
	public BigDecimal getCreditoTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (FundoFixo fundoFixo : (List<FundoFixo>)getPagedResult().getWrappedData()) {
			if (fundoFixo.getCredito() != null) {
				total = total.add(fundoFixo.getCredito());
			}
		}
		
		return total;
	}
	
	public BigDecimal getDebitoTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (FundoFixo fundoFixo : (List<FundoFixo>)getPagedResult().getWrappedData()) {
			if (fundoFixo.getDebito() != null) {
				total = total.add(fundoFixo.getDebito());
			}
		}
		
		return total;
	}
	
	public BigDecimal getSaldo() {
		return getCreditoTotal().subtract(getDebitoTotal());
	}	
	
	public Date getDataDeInicio() {
		return dataDeInicio;
	}

	public void setDataDeInicio(Date dataDeInicio) {
		this.dataDeInicio = dataDeInicio;
	}

	public Date getDataDeTermino() {
		return dataDeTermino;
	}

	public void setDataDeTermino(Date dataDeTermino) {
		this.dataDeTermino = dataDeTermino;
	}
}